from contextlib import contextmanager
import logging

# 注意：此模块不再连接真实数据库，仅提供模拟功能
# 当需要连接数据库时，可以配置下面的参数
# DB_HOST = "localhost"
# DB_PORT = 3306
# DB_USER = "username"
# DB_PASSWORD = "password"
# DB_NAME = "database"

logger = logging.getLogger(__name__)

class DummyCursor:
    """模拟数据库游标"""
    def __init__(self):
        self.queries = []
    
    def execute(self, query, params=None):
        """模拟执行SQL语句"""
        logger.debug(f"模拟执行SQL: {query}")
        if params:
            logger.debug(f"参数: {params}")
        self.queries.append((query, params))
        return 0
    
    def fetchall(self):
        """模拟获取所有结果"""
        logger.debug("模拟获取所有结果")
        return []
    
    def fetchone(self):
        """模拟获取一条结果"""
        logger.debug("模拟获取一条结果")
        return None
    
    def close(self):
        """模拟关闭游标"""
        logger.debug("模拟关闭游标")

@contextmanager
def get_cursor(commit=False):
    """获取模拟数据库游标的上下文管理器"""
    cursor = DummyCursor()
    try:
        yield cursor
        if commit:
            logger.debug("模拟提交事务")
    except Exception as e:
        if commit:
            logger.debug("模拟回滚事务")
        logger.error(f"模拟数据库操作错误: {str(e)}")
        raise
    finally:
        cursor.close() 